package com.mc.code.web.sys;

import com.mc.code.entity.CodeUsers;
import com.mc.code.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 管理用户
 */
@Controller
public class UserManageController {

    //分页
    private static final long serialVersionUID = 1L;
    private static final int PAGE_COUNT = 3;//每页显示记录数

    @Autowired
    private UsersService usersService;

    //模糊查询显示用户列表 + 分页
    @RequestMapping("/userList")
    public String queryUserList(String type, String state,Model model, HttpServletRequest request){
        //默认当前页
        int currentPage = 1;
        String page = request.getParameter("page");
        if(page != null) {
            currentPage = Integer.valueOf(page);
        }
        List<CodeUsers> list = usersService.queryfaintnessUser(type,state,currentPage,PAGE_COUNT);
        int totalPage = usersService.getTotalPage(PAGE_COUNT,state,type);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("list",list);
        model.addAttribute("state",state);
        model.addAttribute("type",type);
        return "sys/userManage";
    }

    //显示用户详细信息
    @RequestMapping("/userDetail")
    public String userDetail(Model model,int userNo){
        CodeUsers codeUsers = usersService.queryByUserNo(userNo);
        model.addAttribute("user",codeUsers);
        return "sys/userDetail";
    }

    //修改封停用户信息
    @RequestMapping("/updateUser")
    public String updateUser(Model model,int userNo){
        CodeUsers codeUsers= usersService.queryByUserNo(userNo);
        usersService.updateCodeUser(codeUsers);
        model.addAttribute("codeUsers",codeUsers);
        return "redirect:/userList";
    }

    //修改未封停用户信息
    @RequestMapping("/modifyUser")
    public String modifyUser(Model model,int userNo){
        CodeUsers codeUsers= usersService.queryByUserNo(userNo);
        usersService.modifyCodeUser(codeUsers);
        model.addAttribute("codeUsers",codeUsers);
        return "redirect:/userList";
    }

    //逻辑删除用户
    @RequestMapping("/deleteCodeUsers")
    @ResponseBody   //声明返回的数据为json格式
    public String deleteCodeUsers(int userNo){
        try{
            System.out.println("delete" + userNo);
            usersService.deleteCodeUserByUserNo(userNo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "success";
    }

}
